import request from '@/api/request'
import type { ApiResponse } from '@/types/http'

// 权限数据类型定义
export interface SysPermissions {
    id: number
    permissionName: string
    routeTitle: string
    routeName: string
    routePath: string
    componentPath: string
    icon: string
    activeRoute: string
    parentId: number
    sortOrder: number
    permissionLevel: number
    isHidden: number
    status: number
    isDeleted: number
    description: string
    createdAt: string
    updatedAt: string
}
export interface PermissionNode extends SysPermissions {
    children?: PermissionNode[]
}

// 菜单列表响应类型
export interface MenuListResponse {
    path: string
    name: string
    meta: {
        title: string
        icon: string
        hidden?: number
        activeRoute: string
        permissionLevel: number
    }
    component?: string
    children?: MenuListResponse[]
}

// 获取权限列表
export function getPermissionListApi(): Promise<ApiResponse<SysPermissions[]>> {
    return request.get('/premiss/list')
}

// 获取菜单列表，暂无参数。默认获取所有菜单
export function getMenuListApi(): Promise<ApiResponse<MenuListResponse[]>> {
    return request.get('/premiss/menu')
}

// 新增菜单列表
export function AddSysPermissionApi(req: Partial<SysPermissions>) {
    return request.post('/premiss', req)
}

// 更新菜单/权限
export function UpdateSysPermissionApi(id: number, req: Partial<SysPermissions>) {
    return request.put(`/premiss`, req)
}

// 删除菜单/权限
export function DeleteSysPermissionApi(id: number) {
    // 接口规范：POST /premiss/{id} 且为 form-urlencoded
    // 但仅路径参数即可，主体可为空
    return request.post(`/premiss/${id}`)
}
